<?php
require_once("DB_Config.php");
$db_connect = DB_Connection::getInstance();

//case district been changed and need to change to city select
if ( $_GET['flag'] == 1){  
		$dis_name = $_GET['dis_name'];
		$q1 = "SELECT * FROM cities WHERE district=?";
		$stmt = $db_connect->db_conn->prepare($q1);
		$param=array($dis_name);
		if ($stmt->execute($param)){
		 	$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
		 	$i = 0; 
		 	?><!--fisrt option for zero -->
		 	<option selected="selected" value=""></option>
		 	<?php while ($i < count($res)) { ?>
		 	 	<option value="<?php echo $res[$i]['city_name']; ?>">
		 	 		<?php echo $res[$i]['city_name']; $i++; ?>
		 	  	</option>
		 	  	<?php
		 	}
		}
}
//case number of minimum rooms been change so we need to update the max num of rooms
if ( $_GET['flag'] == 2){ 
		$minroom = $_GET['minroom'];
		for ($i=$minroom; $i<=12; $i++){
				?>
		 	 	<option value="<?php echo $i; ?>">
		 	 		<?php echo $i; ?>
		 	  	</option>
		 	  	<option value="<?php echo $i+0.5; ?>">
		 	 		<?php echo $i+0.5; ?>
		 	  	</option>
		 	  	<?php
  		}
}

//case number of minimum floor been change so we need to update the max num of floor
if ( $_GET['flag'] == 3){ 
		$min_floor = $_GET['min_floor'];
		if ($min_floor < 0){
			  ?><option value="-1">all</option><?php
		}
		else{
			for ($i=$min_floor; $i<=19; $i++){
					?>
			 	 	<option value="<?php echo $i; ?>">
			 	 		<?php echo $i; ?>
			 	  	</option>
			 	  	<?php
	  		}
	  		?><option value="20">20+</option><?php
	  	}
}
?>


<?php
if ( $_GET['flag'] == 4){ 
		$city = $_GET['city'];
		$apparstr = $_GET['apparstr'];

		$address1 = $apparstr;
		$address1 = $address1.", ";
		$address1 = $address1.$city;
		// echo $address1."<br>";
		//$address1 = "haifa, hanamal 4";
		//echo $address1;
		//find the lan & let by address
				$myaddress = urlencode($address1);
				//here is the google api url
				$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$myaddress&sensor=false";
				//get the content from the api using file_get_contents
				$getmap = file_get_contents($url); 
				//the result is in json format. To decode it use json_decode
				$googlemap = json_decode($getmap);
				//get the latitute, longitude from the json result by doing a for loop
				
				foreach($googlemap->results as $res){
					 $address = $res->geometry;
					 $latlng = $address->location;
					 $formattedaddress = $res->formatted_address;
					 $lat = $latlng->lat; //latitude
					 $lng = $latlng->lng; //langtitude
					 echo $lat;
					 echo ",";
					 echo $lng;
				}
}
	 ?> 



<?php  
//print cities and set index of the city of the user that wantes to updateit
if ( $_GET['flag'] == 5){ 
	$city_name = $_GET['city_name'];
	$dis_name = $_GET['dis_name'];
		$q1 = "SELECT * FROM cities WHERE district=?";
		$stmt = $db_connect->db_conn->prepare($q1);
		$param=array($dis_name);
		if ($stmt->execute($param)){
		 	$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
		 	$i = 0; 
		 	?><!--fisrt option for zero -->
		 	<option selected="selected" value=""></option>
		 	<?php while ($i < count($res)) { ?>
		 	 	<option value="<?php echo $res[$i]['city_name']; ?>"
		 	 		<?php  if( $city_name==$res[$i]['city_name']){?> selected="selected" <?php  }?> >
		 	 		<?php echo $res[$i]['city_name']; $i++; ?>
		 	  	</option>
		 	  	<?php
		 	}
		}
}



//showing youtube video in search page with md5
if ( $_GET['y_tube_id'] ){ 
	$url_id = $_GET['y_tube_id'];

	$sql="SELECT * FROM youtube_table WHERE youtube_table.y_tube_md5=? ";
	$stmt = $db_connect->db_conn->prepare($sql);
	$param = array($url_id);

	if ($stmt->execute($param)){
		$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
		if (count($row) == 0 ){ $url = "/house_shower/images/no-video.png"; }
		else { $url = $row[0]['y_tube_link']; }	
	}
	else{ $url = "/house_shower/images/no-video.png"; }

	header("Location:$url");
}
?>